<?php
/**
 * @title 338. 比特位计数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer $num
     * @return Integer[]
     */
    function countBits($num) {
        /**
         * @since 2020-08-12 初始值
         */
        $counts = [0];

        /**
         * @since 2020-08-12 每次循环长度
         */
        $len = 1;

        /**
         * @since 2020-08-12 迭代
         */
        while($num>0){
            if($num >= $len){
                $num = $num-$len;
            } else {
                $len = $num;
                $num = 0;
            }

            /**
             * @since 2020-08-12 翻倍叠加, 遍历
             */
            for($i=0; $i<$len; $i++){
                /**
                 * @since 2020-08-12 count++
                 */
                $counts[] = $counts[$i]+1;
            }

            /**
             * @since 2020-08-12 长度翻倍
             */
            $len = $len<<1;
        }

        /**
         * @return
         */
        return $counts;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.12_2.php
 */
$datas = [
    // 0,
    // 1,
    // 2,
    5,
    // pow(10,4)*5,
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->countBits($data);
    if(count($result)<100){
        var_dump($result);
    } else {
        echo "count=", count($result);
    }
    echo str_repeat("<br>", 3);
}
$xhprof->end();
